package com.caijin.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caijin.common.constant.UserConstants;
import com.caijin.common.core.domain.Result;
import com.caijin.common.core.domain.entity.TaskContent;
import com.caijin.common.core.domain.vo.PostStuInfoVO;
import com.caijin.common.core.domain.vo.PostTaskVO;
import com.caijin.system.mapper.TaskContentMapper;
import com.caijin.system.service.ITaskContentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 作业内容表 服务实现类
 * </p>
 *
 * @author Peas
 * @since 2021-05-28
 */
@Service
@SuppressWarnings("all")
public class TaskContentServiceImpl extends ServiceImpl<TaskContentMapper, TaskContent> implements ITaskContentService {

    @Autowired
    private TaskContentMapper mapper;

    @Override
    public Result postTaskContent(TaskContent taskContent) {
        TaskContent taskContent1 = mapper.selectById(taskContent.getId());
        if (taskContent1 != null) {
            return mapper.updateById(taskContent) != 0 ? Result.ok().message("作业修改成功") : Result.error().message("作业修改失败");
        }
        return mapper.insert(taskContent) != 0 ? Result.ok().message("作业提交成功") : Result.error().message("作业提交失败");
    }

    @Override
    public Result getTaskContent(Long sno, Long taskInfoId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("sno", sno);
        queryWrapper.eq("task_info_id", taskInfoId);
        return Result.ok().data("data", mapper.selectOne(queryWrapper));
    }

    @Override
    public Result getPostTaskList(Long taskInfoId) {
        PostTaskVO postTaskVO = new PostTaskVO();
        // 获取提交作业的学生列表
        List<PostStuInfoVO> hasPostStudentList = mapper.selectHasPostStudentList(taskInfoId);
        // 设置提交作业的学生人数
        postTaskVO.setHasPostNum(hasPostStudentList.size());
        // 获取本次作业的所有学生列表
        List<PostStuInfoVO> allStudentList = mapper.selectAllStudentByTaskInfoId(taskInfoId);
        // 设置总人数
        postTaskVO.setCount(allStudentList.size());
        // 去重，获取未提交作业的学生列表
        Map<Long, PostStuInfoVO> data = new HashMap<>();
        hasPostStudentList.stream().forEach(o -> data.put(o.getSno(), o));
        // 将提交作业的学生的hasPost设置为true
        List<PostStuInfoVO> postStuInfoVOList = hasPostStudentList.stream().map(postStuInfoVO -> {
            if (postStuInfoVO.getScore() != null)
                postStuInfoVO.setPostState(UserConstants.TASK_SCORED_STATE);
            else
                postStuInfoVO.setPostState(UserConstants.TASK_POSTED_STATE);
            return postStuInfoVO;
        }).collect(Collectors.toList());
        // 将未提交作业的学生列表与提交作业的学生列表整合
        for (PostStuInfoVO postStuInfoVO : allStudentList) {
            if (!data.containsKey(postStuInfoVO.getSno()))
                postStuInfoVOList.add(postStuInfoVO);
        }
        // 将班级学生列表整合到map中，key:班级名称-value:班级学生列表
        Map<String, List<PostStuInfoVO>> clazzStuListMap = new HashMap<>();
        String cname;
        for (PostStuInfoVO postStuInfoVO : postStuInfoVOList) {
            cname = postStuInfoVO.getCname();
            if (!clazzStuListMap.containsKey(cname)) {
                clazzStuListMap.put(cname, new ArrayList<PostStuInfoVO>());
            }
            clazzStuListMap.get(cname).add(postStuInfoVO);
        }
        // 设置整合后的数据到postTaskVO中，发送前端进行展示
        postTaskVO.setDataMap(clazzStuListMap);
        return Result.ok().data("data", postTaskVO);
    }
}
